<%@
	page language="java" contentType="image/jpeg" pageEncoding="UTF-8"
	import="java.util.*,java.io.*,java.awt.*,java.awt.image.*,javax.imageio.*,cn.edu.nju.software.cdiy.util.Constant"
%>
<%!
	//在此处获取并生成随机颜色
	Color getRandColor(Random random, int ff, int cc) {
		if (ff > 255)
			ff = 255;
		if (cc > 255)
			cc = 255;
		int r = ff + random.nextInt(cc - ff);
		int g = ff + random.nextInt(cc - ff);
		int b = ff + random.nextInt(cc - ff);
		return new Color(r, g, b);
	}
%>
<%
	//在此处 设置JSP页面无缓存
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);
	// 设置图片的长宽
	int width = 100;
	int height = 26;
	//从字库文件中读出字符
	String filePath = request.getSession().getServletContext().getRealPath("/") + "letters.txt";
	BufferedReader reader = new BufferedReader(new FileReader(filePath));
	int charCod;
	java.util.List<Character> letters = new ArrayList<Character>();
	while ((charCod = reader.read()) != -1) {
		letters.add((char) charCod);
	}
	//设置备选随机汉字的个数
	int length = letters.size();
	// 创建缓存图像
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	// 获取图像
	Graphics g = image.getGraphics();
	// 创建随机函数的实例
	Random random = new Random();
	//此处 设定图像背景色
	g.setColor(getRandColor(random, 188, 235));
	g.fillRect(0, 0, width, height);
	//设置随机 备选的字体类型
	String[] fontTypes = {"宋体", "黑体", "楷体", "隶书", "幼圆", "华文新魏", "方正舒体"};
	int fontTypesLength = fontTypes.length;
	// 在图片背景上增加噪点，增加图片分析难度
	g.setColor(getRandColor(random, 180, 199));
	g.setFont(new Font("Times New Roman", Font.PLAIN, 14));
	for (int i = 0; i < 4; i++) {
		g.drawString("!*@*#*$*%*^*&*", 0, 5*(i + 2));
	}
	// 取随机产生的验证码 (4 个汉字 )
	// 保存生成的汉字字符串
	String sRand = "";
	for (int i = 0; i < 4; i++) {
		int start = random.nextInt(length);
		String rand = letters.get(start).toString();
		sRand += rand;
		// 设置图片上字体的颜色
		g.setColor(getRandColor(random, 10, 150));
		// 设置字体格式
		g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)], Font.BOLD, 22 + random.nextInt(6)));
		// 将此汉字画到验证图片上面
		g.drawString(rand, 5 + 20*i + random.nextInt(8), 20);
	}
	// 将验证码存入S ession中
	session.setAttribute(Constant.S_AUTHENTICODE, sRand);
	g.dispose();
	//将 图象输出到JSP页面中
	ImageIO.write(image, "JPEG", response.getOutputStream());
	//关闭流
	out.clear();
	out = pageContext.pushBody();
%>